package com.shopAuth.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shopAuth.bean.Admin;
import com.shopAuth.dao.AdminMapper;
import com.shopAuth.service.IAdminService;

import shopBean.common.Exception.MyException;
import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.RestResultGenerator;
import shopBean.common.utils.Tool;
import shopBean.common.utils.UUIDGenerator;


@Service("adminService")
public class AdminServiceImpl implements IAdminService {

	@Autowired
	AdminMapper adminMapper;
	@Transactional
	public RestResult create(Admin admin) {
		if(adminMapper.getByUser(admin.getUser())!=null){
			return RestResultGenerator.genErrorResult("用户已存在", null, Status.user_exist);
		}
		admin.setId(UUIDGenerator.generate38());
		admin.setPassword(Tool.md5Password(admin.getPassword()));
		adminMapper.insert(admin);
		return RestResultGenerator.genSuccessResult();
	}
	@Transactional
	public RestResult updatePassword(String password, String user) {
		// TODO Auto-generated method stub
		password = Tool.md5Password(password);
		adminMapper.update(user, password);
		return RestResultGenerator.genSuccessResult();
	}

	public RestResult login(String password, String user) {
		Admin admin = adminMapper.getByUser(user);
		if(admin ==null){
			return RestResultGenerator.genErrorResult("用户不存在", null, Status.user_exist);
		}
		if(!Tool.md5Password(password).equals(admin.getPassword())){
			return RestResultGenerator.genErrorResult("账号密码或密码错误", null, Status.user_exist);
		}
		return RestResultGenerator.genSuccessResult();
	}
	
}
